#include "del.h"
#include "ui_del.h"

Del::Del(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Del)
{
    ui->setupUi(this);
}

Del::Del(QTcpSocket * s, QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Del)
{
    ui->setupUi(this);
    UIDesign::setButtonUI(ui->backButton);
    UIDesign::setButtonUI(ui->delButton);
    UIDesign::setLineEditUI(ui->userID);
    UIDesign::setLineEditUI(ui->password);
    UIDesign::setTransparent(ui->label);
    UIDesign::setTransparent(ui->label_2);
    UIDesign::setWindowUI(ui->centralwidget);

    setWindowTitle("注销");
    setWindowOpacity(0.95);
    setFixedSize(this->width(), this->height());

    sock = s;
    disconnect(s, SIGNAL(readyRead()), 0, 0);
}

Del::~Del()
{
    delete ui;
}

void Del::on_delButton_clicked()
{


    disconnect(sock, SIGNAL(readyRead()), 0, 0);
    connect(sock, SIGNAL(readyRead()), this, SLOT(recv()));
    if(ui->userID->text()==""){
        QMessageBox::information(this, "提示", "用户ID不能为空");
        return;
    }
    QMessageBox::StandardButton btn;
    btn = QMessageBox::question(this, "提示", "确定要将此账号注销吗?注销后所有消息将被删除", QMessageBox::Yes|QMessageBox::No);
    if (btn == QMessageBox::Yes) {
        QJsonObject request;
        request.insert("order", 4);
        request.insert("myid", ui->userID->text());
        QJsonObject data;
        data.insert("pwd", ui->password->text());
        request.insert("data", data);
        qDebug() << "---------------------------------------\n";
        qDebug() << request;
        qDebug() << "data:\n";
        qDebug() << data;
        qDebug() << "---------------------------------------\n";
        QJsonDocument document;
        document.setObject(request);
        QByteArray byteRequest = document.toJson();
        sock->write(byteRequest);
    }
}

void Del::on_backButton_clicked()
{
    this->close();
    emit mainclose();
}

void Del::recv()
{
    QByteArray byteRequest;
    byteRequest = sock->readAll();
    QJsonParseError err;
    QJsonDocument document = QJsonDocument::fromJson(byteRequest, &err);
    if(err.error == QJsonParseError::NoError)
    {
        QJsonObject request = document.object();
        qDebug() <<"-----------------------------------------\n";
        qDebug() << request;
        qDebug() << "------------------------------------------\n";
        QJsonValue Rstatus = request.value("status");
        bool status = Rstatus.toBool();
        if(status)
        {
            QMessageBox::information(this, "提示", "已注销！");
            emit mainclose();
            this->close();
        }
        else
        {
            QMessageBox::warning(this, "警告", "注销失败！请检查ID与密码");
        }
    }
    else QMessageBox::warning(this, "警告", "服务器下传失败");
}


